﻿using System.Collections.Specialized;
using System.Configuration;

namespace MyLib.Tools.AutoDbUnit {
	/// <summary>
	/// 
	/// </summary>
	public class TableNameElement : ConfigurationElement {
		/// <summary>
		/// 
		/// </summary>
		public TableNameElement() {
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		public TableNameElement(string name) {
			this.Name = name;
		}

		/// <summary>
		/// 
		/// </summary>
		[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
		public string Name {
			get {
				return (string)this["name"];
			}
			set {
				this["name"] = value;
			}
		}
	} // end of TableNameElement.

	/// <summary>
	/// 
	/// </summary>
	public class TableNameCollection : ConfigurationElementCollection {
		/// <summary>
		/// 
		/// </summary>
		public override ConfigurationElementCollectionType CollectionType {
			get {
				return ConfigurationElementCollectionType.AddRemoveClearMap;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		/// <returns></returns>
		public TableNameElement this[int index] {
			get {
				return (TableNameElement)BaseGet(index);
			}
			set {
				if (BaseGet(index) != null)
					BaseRemoveAt(index);
				BaseAdd(index, value);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="element"></param>
		public void Add(TableNameElement element) {
			BaseAdd(element);
		}

		/// <summary>
		/// 
		/// </summary>
		public void Clear() {
			BaseClear();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected override ConfigurationElement CreateNewElement() {
			return new TableNameElement();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		protected override object GetElementKey(ConfigurationElement element) {
			return (element as TableNameElement).Name;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="element"></param>
		public void Remove(TableNameElement element) {
			BaseRemove(element.Name);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		public void Remove(string name) {
			BaseRemove(name);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		public void RemoveAt(int index) {
			BaseRemoveAt(index);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		internal string[] ToArray() {
			var ret = new string[this.Count];

			for (int i = 0; i < this.Count; ++i) {
				ret[i] = this[i].Name;
			}

			return ret;
		}
	} // end of TableNameCollection.

	/// <summary>
	/// 
	/// </summary>
	public class ExportConfigurationSection : ConfigurationSection {
		/// <summary>
		/// 
		/// </summary>
		private const string MY_SCHEMA_FILE = "test_data.xsd";

		/// <summary>
		/// 
		/// </summary>
		private const string MY_DB_FILE = "test_data.xml";

		/// <summary>
		/// 
		/// </summary>
		public ExportConfigurationSection() {
		}

		/// <summary>
		/// 
		/// </summary>
		[ConfigurationProperty("providerName", IsRequired = true)]
		public string ProviderName {
			get {
				return (string)this["providerName"];
			}
			set {
				this["providerName"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[ConfigurationProperty("connectionString", IsRequired = true)]
		public string ConnectionString {
			get {
				return (string)this["connectionString"];
			}
			set {
				this["connectionString"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[ConfigurationProperty("tables", IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(TableNameCollection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
		public TableNameCollection Tables {
			get {
				return (TableNameCollection)base["tables"];
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[ConfigurationProperty("schemaFileName", IsRequired = false, DefaultValue = MY_SCHEMA_FILE)]
		public string SchemaFileName {
			get {
				return (string)this["schemaFileName"];
			}
			set {
				this["schemaFileName"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[ConfigurationProperty("dataFileName", IsRequired = false, DefaultValue = MY_DB_FILE)]
		public string DataFileName {
			get {
				return (string)this["dataFileName"];
			}
			set {
				this["dataFileName"] = value;
			}
		}
	} // end of ExportConfigurationSection.
}
